/*
 * Copyright (c) 2008-20018 Saša Stamenković <umpirsky@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    * Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright notice,
 *    	this list of conditions and the following disclaimer in the documentation
 *    	and/or other materials provided with the distribution.
 *    * Neither the name of the <ORGANIZATION> nor the names of its contributors
 *    	may be used to endorse or promote products derived from this software without
 *    	specific prior written permission.
 *
 *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *    IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 *    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 *    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 *    OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * Javascript api umpirsky provides basic interaction
 * api to java applet (umpirsky uploader).
 * You can register events, attach handlers, call applet
 * methods through this simple javascript api.
 * @see http://uploader.umpirsky.com/
 * @see http://code.google.com/p/umpirsky-uploader/
 */
var umpirsky = function() {

	var version = 1.0;

	return {
		/**
		 * List of events
		 */
		events: {
			/**
			 * Triggered on error, when a system error occurs.
			 * Can be used to initiate old school upload.
			 */
			onError : null,

			/**
			 * Triggered when applet is loaded.
			 */
			onLoad : null,

			/**
			 * Triggered whenever file list is updated
			 */
			fileListUpdated : null,

			/**
			 * Triggered whenever upload progress is changed.
			 * @param int percent - progress in %
			 */
			uploadProgressListener : null,

			/**
			 * Triggered when file upload finished.
			 */
			uploadFinished : null
		},

		/**
		 * Array of files that are readt for upload.
		 * One array element structure:
		 * {
		 * 	id : int
		 * 	name : String
		 *  size : int
		 * 	date : String
		 * }
		 */
		files : new Array(),

		/**
         * Adds event listener
         * @param String eventName - name of event
         * @param function eventHandler - function to handle event
         * @return void
         */
		addEventListener : function(eventName, eventHandler) {
			if (('undefined' != typeof(this.events[eventName])) && ('function' == typeof(eventHandler))) {
				this.events[eventName] = eventHandler;
			}
		},

		/**
         * Triggers browse action, call applet to show file chooser. Updates file list (umpirsky.files).
         * @return void
         */
		browse : function() {
			document.UmpirskyUploader.browse();
		},

		/**
         * Remove one file (by id) from list (umpirsky.files)
         * @param String id - file id
         * @return void
         */
		removeFile : function(id) {
			document.UmpirskyUploader.removeFile(id)
		},

		/**
         * Remove all files the list (umpirsky.files)
         * @return void
         */
		clearFiles : function() {
			document.UmpirskyUploader.clearFiles()
			this.files = new Array();
		},

		/**
         * Called when file list is updated. Updates files list (umpirsky.files)
         * @return void
         */
		fileListUpdated : function() {
			this.files = eval("(" + document.UmpirskyUploader.getFiles() + ")");
			if ('function' == typeof(this.events.fileListUpdated)) {
				this.events.fileListUpdated();
			}
		},

		/**
         * Triggers upload action. All files from list (umpirsky.files) will be sent to server (applet.url).
         * @return String response from server
         */
		upload : function() {
			return document.UmpirskyUploader.upload();
		},

		/**
		 * Determines is java enabled in clients browser
		 * @return bool true if java is enabled
		 */
		isJavaEnabled : function(){
			try {
				return (navigator.javaEnabled && navigator.javaEnabled());
			} catch (e) {
				return true;
			}
		}
	}
}();